#pragma once
#include "Comman.h"

template<class T>
class ObjectPool
{
public:
	T* New()
	{
		T* obj = nullptr;
		
		if (_freeList)
		{
			void* next = NextObj(_freeList);
			obj = (T*)_freeList;
			_freeList = next;
		}
		else
		{
			if (_remainSize < sizeof(T))
			{
				_remainSize = 128 * 1024;
				//_memory = (char*)malloc(_remainBytes);
				_memory = (char*)SystemAlloc(_remainSize >> 13);
				if (_memory == nullptr)
				{
					throw std::bad_alloc();
				}
			}
++
			obj = (T*)_memory;
			size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);
			_remainSize -= objSize;
			_memory += objSize;
		}
		
		new(obj)T;


		return obj;
	}

	void Delte(T* obj)
	{
		obj->~T();
		NextObj(obj) = _freeList;
		_freeList = obj;
	}

private:
	char* _memory = nullptr;
	size_t _remainSize = 0;
	void* _freeList = nullptr;
};

